package com.streambolics.mail;

import com.streambolics.std.Invoker;

public class BeanMimeParser extends MimeParser
{
    private final Invoker _Target;
    private boolean       _Strict;

    public BeanMimeParser(Object aTarget)
    {
        _Target = new Invoker (aTarget);
    }

    public BeanMimeParser(Object aTarget, boolean aStrict)
    {
        _Target = new Invoker (aTarget);
        _Strict = aStrict;
    }

    @Override
    protected void dispatch (String aMimeType, String aMimeSubType, String aContent) throws Exception
    {
        if (_Target.invokeOptional ("setMime_" + aMimeType + "_" + aMimeSubType, aContent) && !_Target.invokeOptional ("setMime_" + aMimeType, aContent))
        {
            reportUnparsable (aMimeType, aMimeSubType);
        }
    }

    protected void reportUnparsable (String aMimeType, String aMimeSubType) throws Exception
    {
        if (_Strict)
        {
            throw new Exception ("Could not parse message part " + aMimeType + "/" + aMimeSubType);
        }
    }
}
